import 'package:environmental_les_app/common/style/colours.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';

import 'button_public_rect.dart';

class BottomSelectDrawer extends GetView<BottomSelectDrawerController> {
  const BottomSelectDrawer({
    super.key,
    required this.title,
    required this.contentList,
    required this.completeSelect,
  });

  final String title;

  final List contentList;

  //修改成功回传结果
  final CompleteSelect completeSelect;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<BottomSelectDrawerController>(
      init: BottomSelectDrawerController(),
      id: "bottom_select_drawer",
      builder: (_) {
        return _buildBody();
      },
    );
  }

  Widget _buildBody() {
    //title and close button
    var titleRow = Row(
      children: [
        const Spacer(),
        Text(
          title,
          style: const TextStyle(
              fontSize: 18.0, color: Colors.black, fontWeight: FontWeight.w600),
        ),
        const Spacer(),
        IconButton(
          onPressed: () {
            Get.back();
          },
          icon: Container(
            height: 30.0,
            width: 30.0,
            decoration: const BoxDecoration(
              shape: BoxShape.circle,
              color: Colours.lineE5E6EB,
            ),
            child: const Icon(
              Icons.close,
              size: 20.0,
              color: Colours.text969,
            ),
          ),
        ),
        hGap10,
      ],
    );

    var contentListWidget = List.generate(
        contentList.length,
        (index) => InkWell(
              onTap: () {
                controller.selectItemIndex(index);
                controller.updateView();
              },
              child: Container(
                height: 48.0,
                margin: const EdgeInsets.only(left: 10, right: 10, top: 10),
                padding: const EdgeInsets.symmetric(horizontal: 15.0),
                decoration: const BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(8.0)),
                ),
                child: Row(
                  children: [
                    Text(
                      contentList[index],
                      style: const TextStyle(
                          fontSize: 15.0,
                          color: Colours.text254,
                          fontWeight: FontWeight.w600),
                    ),
                    const Spacer(),
                    controller.selectIndex == index
                        ? Container(
                            height: 16.5,
                            width: 16.5,
                            decoration: const BoxDecoration(
                              shape: BoxShape.circle,
                              color: Colours.appMain,
                            ),
                            child: const Icon(
                              Icons.check,
                              size: 13.0,
                              color: Colors.white,
                            ),
                          )
                        : Container(
                            height: 16.5,
                            width: 16.5,
                            decoration: const BoxDecoration(
                              shape: BoxShape.circle,
                              color: Colors.white,
                              border: Border.fromBorderSide(
                                  BorderSide(color: Colours.textC9CDD4)),
                            ),
                          ),
                  ],
                ),
              ),
            ));

    // 完成按钮
    var completeButton = Container(
      margin: const EdgeInsets.only(top: 10.0),
      height: 90.0,
      padding: const EdgeInsets.only(
          left: 15.0, right: 15.0, bottom: 30.0, top: 10.0),
      decoration: const BoxDecoration(color: Colors.white),
      child: ButtonPublicRect(
        minWidth: double.infinity,
        onPressed: () {
          //回调结果
          Get.back();
          completeSelect.call(
              contentList[controller.selectIndex], controller.selectIndex);
        },
        text: "完成",
      ),
    );

    return Container(
      decoration: const BoxDecoration(
          color: Colours.bgColor,
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(12.0), topRight: Radius.circular(12.0))),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          titleRow,
          ListView(
            shrinkWrap: true,
            children: contentListWidget,
          ),
          completeButton
        ],
      ),
    );
  }
}

// 组件控制器
class BottomSelectDrawerController extends GetxController {
  BottomSelectDrawerController();

  // 选中的item index
  var selectIndex = -1;

  // 选中item
  void selectItemIndex(int index) {
    selectIndex = index;
  }

  // 刷新
  void updateView() {
    update(["bottom_select_drawer"]);
  }
}

typedef CompleteSelect = Function(dynamic data, int index);
